<?php

error_reporting(E_ALL | E_STRICT);
set_include_path('../library' . PATH_SEPARATOR . get_include_path());

require_once 'Zend/Filter/ConvertNewlines.php';


echo '---convert newlines---';
$convert = new Zend_Filter_ConvertNewlines('windows');
$a = "a\rb\r";
$b = $convert->filter($a);

var_dump(array_map('ord', str_split($b)));

$lorem = file_get_contents('lorem.txt');

require_once 'Zend/Filter/Unix/Head.php';


echo PHP_EOL . '-----head 8 lines-----' . PHP_EOL;
$head = new Zend_Filter_Unix_Head(8);
echo $head->filter($lorem);

echo PHP_EOL . '-----head 20 words-----' . PHP_EOL;
$head = new Zend_Filter_Unix_Head(20, 'word');
echo $head->filter($lorem);

echo PHP_EOL . '-----head 7 bytes------' . PHP_EOL;
$head = new Zend_Filter_Unix_Head(7, 'byte');
echo $head->filter($lorem);

require_once 'Zend/Filter/Unix/Tail.php';

echo PHP_EOL . '-----tail 3 lines------' . PHP_EOL;
$tail = new Zend_Filter_Unix_Tail(3);
echo $tail->filter($lorem);

echo PHP_EOL . '-----tail 4 words------' . PHP_EOL;
$tail = new Zend_Filter_Unix_Tail(4, 'word');
echo $tail->filter($lorem);

echo PHP_EOL . '-----tail 4 bytes------' . PHP_EOL;
$tail = new Zend_Filter_Unix_Tail(4, 'byte');
echo $tail->filter($lorem);

require_once 'Zend/Filter/Unix/Grep.php';

echo PHP_EOL . '-----grep "um" lines-----' . PHP_EOL;
$grep = new Zend_Filter_Unix_Grep('/um/');
echo $grep->filter($lorem);

echo PHP_EOL . '-----grep lines starting with V and L------' . PHP_EOL;
$grep = new Zend_Filter_Unix_Grep('/^[LV]/');
echo $grep->filter($lorem);


echo PHP_EOL . '----grep A and c bytes-----' . PHP_EOL;
$grep = new Zend_Filter_Unix_Grep('/[Ac]/', false, 'byte');
echo $grep->filter($lorem);

require_once 'Zend/Filter/Unix/Unique.php';

echo PHP_EOL . '----unique----' . PHP_EOL;
$unique = new Zend_Filter_Unix_Unique;
echo $unique->filter($lorem);

require_once 'Zend/Filter/Unix/Sort.php';

echo PHP_EOL . '----sort-----' . PHP_EOL;

$unique = new Zend_Filter_Unix_Sort;
echo $unique->filter($lorem);

echo PHP_EOL . '---CHAIN----'. PHP_EOL;

class Filter_Null implements Zend_Filter_Interface
{
    public function filter($value)
    {
        return $value;
    }
}

$filters = array('lorem' => array(array('Unix_Head', 20),
                                  array('Unix_Grep', '/um|us/'),
                                  'Unix_Unique',
                                  'Unix_Sort'));
                                  
require_once 'Zend/Filter/Input.php';

$input = new Zend_Filter_Input($filters, null, array('lorem' => $lorem), array('escapeFilter' => new Filter_Null));

echo $input->lorem;